package com.ztgf.search.api.client;


import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.utils.CustomizeUtil;
import com.ztgf.search.models.bean.SearchRequestBean;
import com.ztgf.search.models.bean.SearchResponseBean;
import com.ztgf.search.models.dto.EsGoodsDTO;
import com.ztgf.search.models.dto.EsShopDTO;
import com.ztgf.search.models.service.ElasticsearchService;
import com.ztgf.search.models.service.IShopSearchService;
import com.ztgf.search.models.service.SearchService;
import com.ztgf.shopping.models.service.epg.IEpgActualSearchService;
import com.ztgf.shopping.models.service.epg.IEpgHotWordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;

/**
 * 搜索引擎搜索请求入口
 *
 * @author chenyu.wang
 * @version 1.0
 */
@RestController
@RequestMapping("/api/goodsSearch")
@Api(value = "SearchController", tags = "ES商品搜索")
public class SearchController {

    @DubboReference
    private SearchService searchService;
    @DubboReference
    private ElasticsearchService elasticsearchService;
    @DubboReference
    private IEpgHotWordService epgHotWordService;
    @DubboReference
    private IEpgActualSearchService epgActualSearchService;
    @DubboReference
    private IShopSearchService shopSearchService;
    // 别名
    private static final String searchGoodsAlias = "index_goods_2020";

    private static final String searchGoodsType = "goods";
    // 权重
    private static final String searchGoodsField = "goodsNameIkSmart^8,goodsNameIkMaxWord^8,brandNameIkSmart^7,brandNameIkMaxWord^4";
    // 索引名称
    private static final String INDEX_NAME = "index_goods";
    // 店铺别名
    private static final String searchShopAlias = "index_shop_2020";

    private static final String searchShopType = "shop";

    private static final String searchShopField = "shopNameIkSmart^8,shopNameIkMaxWord^5";
    // 索引名称
    private static final String INDEX_NAME_SHOP = "index_shop";


    /**
     * 搜索商品入口
     *
     * @param searchRequestBean
     * @return
     */
    @PostMapping("/queryGoodsSearch")
    @ApiOperation(value = "商品搜索", notes = "商品搜索")
    public Result<?> searchGoods(SearchRequestBean searchRequestBean) {
        if (StringUtils.isNotBlank(searchRequestBean.getGoodsKeyWord())) {
            //添加搜索词
            epgActualSearchService.addOrUpdateNum(searchRequestBean.getGoodsKeyWord());
            //添加热词
            epgHotWordService.updateHotWordOnceByName(searchRequestBean.getGoodsKeyWord());
        }
        SearchResponseBean response = searchService.searchGoods(searchGoodsAlias, searchGoodsType, searchGoodsField, searchRequestBean);
//        if(CustomizeUtil.listIsNull(response.getBrandList())){
//            return new Result<>(new Page<>());
//        }
        Page<EsGoodsDTO> page = new Page<>();
        if (response.getListGoodsBean() != null && response.getListGoodsBean().size() > 0) {
            page.setRecords(response.getListGoodsBean());
        }
        page.setTotal(response.getTotalNum().intValue());
        page.setSize(response.getSize().intValue());
        page.setCurrent(response.getPageNum().intValue());
        return new Result<>(page);
    }

    @PostMapping("/queryShopSearch")
    @ApiOperation(value = "店铺搜索", notes = "店铺搜索")
    public Result<?> queryShopSearch(SearchRequestBean searchRequestBean) {
        if (StringUtils.isNotBlank(searchRequestBean.getGoodsKeyWord())) {
            //添加搜索词
            epgActualSearchService.addOrUpdateNum(searchRequestBean.getGoodsKeyWord());
            //添加热词
            epgHotWordService.updateHotWordOnceByName(searchRequestBean.getGoodsKeyWord());
        }
        SearchResponseBean response = shopSearchService.searchShop(searchShopAlias, searchShopType, searchShopField, searchRequestBean);
        Page<EsShopDTO> page = new Page<>();
        if (null != response.getListShopBean() && response.getListShopBean().size() > 0) {
            page.setRecords(response.getListShopBean());
        }
        page.setTotal(response.getTotalNum().intValue());
        page.setSize(response.getSize().intValue());
        page.setCurrent(response.getPageNum().intValue());
        return new Result<>(page);
    }

    @PostMapping("/addMappingAndAddDate")
    @ApiOperation(value = "添加映射和添加数据", notes = "添加映射和添加数据")
    public Result<?> addMappingAndAddDate(String indexName, Integer type) {
        boolean flag = searchService.addMappingAndAddDate(indexName, type);
        return new Result<>(flag);
    }

}
